﻿
using FluentScheduler;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using NLog.Web;
using OddUserManager.Data.IRepositories;
using OddUserManager.Data.Repositories;
using OddUserManager.Model;
using OddUserManager.Service.IServices;
using OddUserManager.Service.Services;
using OddUserManager.Web.Common;

namespace OddUserManager.Web
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            services.AddAuthentication(options =>
            {
                options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            })
            .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, m =>
            {
                m.Cookie.Name = "OddUserManagerAuthCookie";
                m.LoginPath = new PathString("/Login/Index");
                m.Cookie.Path = "/";
                m.Cookie.HttpOnly = true;
                

            });
            services.AddMemoryCache();

            services.AddMvc();
            DatabaseSettings.DefaultConnectionString = Configuration.GetConnectionString("DefaultConnection");
            DatabaseSettings.JCRealTimeMatchConnectionString = Configuration.GetConnectionString("JRealTimeCMatchConnectionString");
            DatabaseSettings.MacroMatchsDataConnectionString = Configuration.GetConnectionString("MacroMatchsDataConnectionString");
            //services.Configure<ConnectionStrings>(this.Configuration.GetSection("ConnectionStrings"));


            services.AddScoped<IAppUserRepository, AppUserRepository>();
            services.AddScoped<IAppUserService, AppUserService>();
            services.AddScoped<IMenuRepository, MenuRepository>();
            services.AddScoped<IMenusService, MenusService>();
            services.AddScoped<IRoleRepository, RoleRepository>();
            services.AddScoped<IRoleService, RoleService>();
            //services.AddScoped<ILeaguesExplainRepository, LeaguesExplainRepository>();

            services.AddScoped<IOddUserRepository, OddUserRepository>();
            services.AddScoped<IOddUserService, OddUserService>();

            services.AddScoped<IOddRepository, OddRepository>();
            services.AddScoped<IOddService, OddService>();
            services.AddScoped<IRealTimeJCMatchRepository, RealTimeJCMatchRepository>();
            services.AddScoped<IRealTimeJCMatchService, RealTimeJCMatchService>();

            services.AddScoped<IJCTestStrategyGroupRepository, JCTestStrategyGroupRepository>();
            services.AddScoped<IJCTestStrategyGroupService, JCTestStrategyGroupService>();

            //services.AddScoped<IRebateSettlementRepository, RebateSettlementRepository>();
            //services.AddScoped<IRebateSettlementService, RebateSettlementService>();

            //services.AddScoped<IRebateSettingsRepository, RebateSettingsRepository>();
            //services.AddScoped<IRebateSettingsService, RebateSettingsService>();

            //services.AddScoped<IFundDispatchRepository, FundDispatchRepository>();
            //services.AddScoped<IFundDispatchService, FundDispatchService>();

            services.AddScoped<ISiteRepository,SiteRepository>();
            services.AddScoped<ISiteService, SiteService>();

	        services.AddScoped<IAnalystSelectedMatchRepository, AnalystSelectedMatchRepository>();
            services.AddScoped<IAnalystSelectedMatchService, AnalystSelectedMatchService>();


            services.AddScoped<IRealTimeJCMatchRepository, RealTimeJCMatchRepository>();
            services.AddScoped<IRealTimeJCMatchService, RealTimeJCMatchService>();

            services.AddScoped<IBookMarkRepository, BookMarkRepository>();
            services.AddScoped<IBookMarkService, BookMarkService>();
            
            services.AddScoped<ISystemConstantRepository, SystemConstantRepository>();
            services.AddScoped<ISystemConstantService, SystemConstantService>();

            services.AddScoped<IRealTimeMatchsRepository, RealTimeMatchsRepository>();
            services.AddScoped<IRealTimeMatchsService, RealTimeMatchsService>();

            services.AddScoped<IRealTimeTeamRemarkInfoRepository, RealTimeTeamRemarkInfoRepository>();
            services.AddScoped<IRealTimeTeamRemarkInfoService, RealTimeTeamRemarkInfoService>();


            //services.AddScoped<IReportRepository, ReportRepository>();
            //services.AddScoped<IReportService, ReportService>();

            //services.AddScoped<ILeaguesExplainService, LeaguesExplainService>();

            //services.AddScoped<ISimilarWordAssociationService, SimilarWordAssociationService>();

            //services.AddScoped<IMatchExplainService, MatchExplainService>();
            //services.AddScoped<IMatchExplainRepository, MatchExplainRepository>();
            //services.AddScoped<ITeamExplainService, TeamExplainService>();
            //services.AddScoped<ITeamExplainRepository, TeamExplainRepository>();

            //services.AddScoped<IHostGuestTeamExplainService, HostGuestTeamExplainService>();
            //services.AddScoped<IHostGuestTeamExplainRepository, HostGuestTeamExplainRepository>();

            //services.AddScoped<IPersonalResultsService, PersonalResultsService>();
            //services.AddScoped<IPersonalResultsRepository, PersonalResultsRepository>();
            JobManager.Initialize(new MyTaskRegistry());
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            //添加log日志
            loggerFactory.ConfigureNLog("nlog.config");
            loggerFactory.AddNLog();
            app.AddNLogWeb();


            if (env.IsDevelopment())
            {
                app.UseBrowserLink();
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

           
            app.UseStaticFiles();
            app.UseAuthentication();
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}
